package com.sx.pipeline.ui.mine

import android.app.Application
import android.text.TextUtils
import androidx.databinding.ObservableField
import com.blankj.utilcode.util.ToastUtils
import com.sx.mvvmlibs.base.BaseViewModel
import com.sx.mvvmlibs.event.SingleLiveEvent
import com.sx.mvvmlibs.http.CommonObserver
import com.sx.mvvmlibs.utils.RxThreadHelper
import com.sx.pipeline.app.model.CYBaseViewModel
import com.sx.pipeline.repos.http.req.JoinBusinessDTO
import com.sx.pipeline.repos.http.res.Ipage
import com.sx.pipeline.repos.http.res.UnitNameVO
import com.sx.pipeline.repos.http.res.UnitTypeVO
import io.reactivex.rxjava3.core.Observable

/**
 * @Author: YY
 * @Date: 2022/6/27
 * @Desc: TODO
 */
class JoinBusinessVM(app: Application): CYBaseViewModel(app) {

    // 单位类型ID
    val unitTypeIdsField: ObservableField<String> = ObservableField("")
    // 单位类型名称
    val unitTypeNameField: ObservableField<String> = ObservableField("")

    /**
     * 获取单位类型
     */
    val unitTypeListEvent: SingleLiveEvent<List<UnitTypeVO>?> = SingleLiveEvent()
    fun getUnitType() {
        addSubscribe(mModel.queryUnitType()
            .compose(RxThreadHelper.rxSchedulerHelper())
            .subscribeWith(object : CommonObserver<List<UnitTypeVO>, BaseViewModel<*>>
                (this, true) {
                override fun onResult(result: List<UnitTypeVO>) {
                    unitTypeListEvent.postValue(result)
                }

                override fun onFailed(code: Int, msg: String?) {
                    super.onFailed(code, msg)
                    ToastUtils.showShort("$msg")
                    unitTypeListEvent.postValue(null)
                }
            })
        )
    }

    /**
     * 查询单位列表
     */
    val unitNameListEvent: SingleLiveEvent<List<UnitNameVO>> = SingleLiveEvent()

    // 注册身份
    val identityField: ObservableField<Int> = ObservableField()
    // 省ID
    val provinceIDField: ObservableField<Int> = ObservableField()
    // 市ID
    val cityIDField: ObservableField<Int> = ObservableField()
    // 区ID
    val areaIDField: ObservableField<Int> = ObservableField()
    // 详细地址
    val addressField: ObservableField<String> = ObservableField()

    // 选择得单位id
    val selectUnitIdField: ObservableField<Long> = ObservableField(0L)
    // 单位名称
    val unitNameField: ObservableField<String> = ObservableField("")

    fun getUnitNameList() {

        if (identityField.get() == null) {
            ToastUtils.showShort("请选择身份类型")
            return
        }
//        if (TextUtils.isEmpty(unitTypeIdsField.get())) {
//            ToastUtils.showShort("请选择单位类型")
//            return
//        }
        if (areaIDField.get() == null) {
            ToastUtils.showShort("请选择省市区")
            return
        }

        addSubscribe(
            mModel.getUnitNameList(identity = identityField.get()!!, areaId = areaIDField.get()!!,
                unitTypeId = unitTypeIdsField.get() ?: "", isPage = false)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Ipage<UnitNameVO>, BaseViewModel<*>>
                    (this, false) {
                    override fun onResult(result: Ipage<UnitNameVO>) {
                        unitNameListEvent.postValue(result.records)
                        dismissLoadingDialog()
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        dismissLoadingDialog()
                        ToastUtils.showShort(msg)
                    }
                })
        )
    }

    fun joinBusiness(remark: String?="") {
        if (TextUtils.isEmpty(identityField.get().toString())) {
            ToastUtils.showShort("请选择身份")
            return
        }
        if (selectUnitIdField.get() == 0L) {
            ToastUtils.showShort("请选择单位")
            return
        }
        val dto = JoinBusinessDTO(identityField.get().toString(), selectUnitIdField.get()!!,remark)
        addSubscribe(mModel.joinBusiness(dto)
            .compose(RxThreadHelper.rxSchedulerHelper())
            .subscribeWith(object : CommonObserver<Any, BaseViewModel<*>>
                (this, true) {
                override fun onResult(result: Any) {
                    ToastUtils.showLong(result as String)
                    dismissLoadingDialog()
                    finish()
                }

                override fun onFailed(code: Int, msg: String?) {
                    super.onFailed(code, msg)
                    dismissLoadingDialog()
                    ToastUtils.showShort(msg)
                }
            })
        )
    }
}